mod common;
use crate::common::*;
use crate::psn::ck_psn;
use serde_json::json;

/* 1. add tech
 * 2. add task
 * 3. list psn
 * 4. get psn
 * 5. add psn
 * 6. list psn
 * 7. get psn
 * 8. remove psn
 * 9. list psn
 * 10. get psn
 * 11. remove psn
 */
#[tokio::test]
async fn test_psn_1() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "te1",
                "name": "name_te1",
            }
        }),
        "tech",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "task1",
                "pstart": "2024-01-01 01:01:01",
                "pend": "2024-02-01 01:01:01",
                "tech": "te1",
                "pnum": 3,
                "state": "open",
            }
        }),
        "task",
        "add",
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "psn",
        "list",
        e_node(),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
        }),
        "psn",
        "get",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": [{"sn": "psn1"}],
        }),
        "psn",
        "add",
    )
    .await;

    ck_psn(
        pos!(),
        json!({
            "task": ["psn1"],
            "psn1": {"sn": "psn1", "task": "task1"},
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "psn",
        "list",
        json!([{
            "sn": "psn1",
            "task": "task1",
        }]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
        }),
        "psn",
        "get",
        json!({
            "sn": "psn1",
            "task": "task1",
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
        }),
        "psn",
        "rm",
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "s": "2024-01-01 09:09:09",
            "e": "2024-01-01 09:09:09",
        }),
        "task",
        "list",
        json!([{
            "sn": "task1",
            "pstart": "2024-01-01 01:01:01",
            "pend": "2024-02-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "open",
        }]),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "psn",
        "list",
        e_node(),
    )
    .await;

    ck_psn(pos!(), json!({})).await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
        }),
        "psn",
        "get",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
        }),
        "psn",
        "rm",
    )
    .await;

    ck_psn(pos!(), json!({})).await;
}

/* 1. add psn
 * 2. get psn
 * 3. modify psn
 * 4. get psn
 * 5. remove psn
 */
#[tokio::test]
async fn test_psn_2() {
    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "s": "2024-01-01 09:09:09",
            "e": "2024-01-01 09:09:09",
        }),
        "task",
        "list",
        json!([{
            "sn": "task1",
            "pstart": "2024-01-01 01:01:01",
            "pend": "2024-02-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "open",
        }]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": [{"sn": "psn1"}],
        }),
        "psn",
        "add",
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
        }),
        "psn",
        "get",
        json!({
            "sn": "psn1",
            "task": "task1",
        }),
    )
    .await;

    ck_psn(
        pos!(),
        json!({
            "task": ["psn1"],
            "psn1": {"sn": "psn1", "task": "task1"},
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
            "v": {
                "state": 1
            }
        }),
        "psn",
        "md",
    )
    .await;

    ck_psn(
        pos!(),
        json!({
            "task": ["psn1"],
            "psn1": {"sn": "psn1", "task": "task1", "state": 1},
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
        }),
        "psn",
        "get",
        json!({
            "sn": "psn1",
            "task": "task1",
            "state": 1,
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "psn1",
        }),
        "psn",
        "rm",
    )
    .await;

    ck_psn(pos!(), json!({})).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": [{"sn": "psn1"}, {"sn": "psn2"}],
        }),
        "psn",
        "add",
    )
    .await;

    ck_psn(
        pos!(),
        json!({
            "task": ["psn1", "psn2"],
            "psn1": {"sn": "psn1", "task": "task1"},
            "psn2": {"sn": "psn2", "task": "task1"},
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": [{"sn": "psn1"}],
        }),
        "psn",
        "add",
        e_psn("psn repeat: psn1"),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": [{"sn": "psn3"}],
        }),
        "psn",
        "add",
    )
    .await;

    ck_psn(
        pos!(),
        json!({
            "task": ["psn1", "psn2", "psn3"],
            "psn1": {"sn": "psn1", "task": "task1"},
            "psn2": {"sn": "psn2", "task": "task1"},
            "psn3": {"sn": "psn3", "task": "task1"},
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": ["psn5"],
        }),
        "psn",
        "add",
        e_para("psn count must le pnum"),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "task",
        "rm",
    )
    .await;

    ck_psn(pos!(), json!({})).await;
}
